package com.shuyun.ccms.encrypt.common;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.concurrent.TimeUnit;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.shuyun.ccms.encrypt.util.PropertyUtil;

public class ExecuteShell {
	private static Logger logger = LoggerFactory.getLogger(ExecuteShell.class);
	public static void execute(String platForm) throws Exception{
		logger.info("执行平台:" + platForm + ",客户信息shell开始");
		String shellPath = PropertyUtil.getValue("file.path");
		shellPath = shellPath + "/test/script/" + platForm + "/" + FileName.CUSTOMER_INFO_SHELL.getValue();
		try {
			long start = System.currentTimeMillis();
			Process process = Runtime.getRuntime().exec(shellPath);
			 InputStream in = process.getInputStream();
			 BufferedReader br = new BufferedReader(new InputStreamReader(in));
			 String line = br.readLine();
		//	 logger.info("执行平台:" + platForm + ",客户信息shell结束,result:  " + line);
		/*	 if(!line.equals("1")){
				 throw new Exception("执行客户信息shell失败.shell返回值: " + line);
			 }*/
			 process.waitFor(90L,TimeUnit.MINUTES);
			 long end = System.currentTimeMillis();
			// Thread.sleep(1000);
			 logger.info("执行平台:" + platForm + ",客户信息shell结束,耗时: " + (end-start) + "ms,result: "+line);
		} catch (Exception e) {
			logger.error("执行平台:" + platForm + ",客户信息shell失败.shellPath: " + shellPath +",error: " + e.getMessage());
			throw e;
		}
	}

	public static void executePhoneShell(String platForm) throws Exception{
		logger.info("执行平台:" + platForm + ",手机号码shell开始");
		String shellPath = PropertyUtil.getValue("file.path");
		shellPath = shellPath + "/test/script/" + platForm + "/" + FileName.CUSTOMER_MOBILE_SHELL.getValue();
		try {
			long start = System.currentTimeMillis();
			Process process = Runtime.getRuntime().exec(shellPath);
			 InputStream in = process.getInputStream();
			 BufferedReader br = new BufferedReader(new InputStreamReader(in));
			 String line = br.readLine();
			// logger.info("执行平台:" + platForm + ",客户手机号码shell结束,result:  " + line);
		/*	 if(!line.equals("1")){
				 throw new Exception("执行手机号码shell失败. shell返回值: " + line);
			 }*/
			 process.waitFor(90L,TimeUnit.MINUTES);
			 long end = System.currentTimeMillis();
			 //Thread.sleep(1000);
			 logger.info("执行平台:" + platForm + ",客户信息shell结束,耗时: " + (end-start) + "ms,result: "+line);
		} catch (Exception e) {
			logger.error("执行平台:" + platForm + ",手机号码shell失败.shellPath: " + shellPath +",error: " + e.getMessage());
			throw e;
		}
	}

	public static void executeOrderShell(String platForm) throws Exception{
		logger.info("执行平台:" + platForm + ",订单shell开始");
		String shellPath = PropertyUtil.getValue("file.path");
		shellPath = shellPath + "/test/script/" + platForm + "/" + FileName.ORDER_INFO_SHELL.getValue();
		try {
			long start = System.currentTimeMillis();
			Process process = Runtime.getRuntime().exec(shellPath);
			 InputStream in = process.getInputStream();
			 BufferedReader br = new BufferedReader(new InputStreamReader(in));
			 String line = br.readLine();
		//	 logger.info("执行平台:" + platForm + ",订单shell结束,result:  " + line);
		/*	 if(!line.equals("1")){
				 throw new Exception("执行平台:" + platForm + ",订单shell失败. shell返回值: " + line);
			 }*/
			 process.waitFor(90L,TimeUnit.MINUTES);
			 long end = System.currentTimeMillis();
			// Thread.sleep(1000);
			 logger.info("执行平台:" + platForm + ",客户信息shell结束,耗时: " + (end-start) + "ms,result: "+line);
		} catch (Exception e) {
			logger.error("执行平台:" + platForm + ",订单shell失败.shellPath: " + shellPath +",error: " + e.getMessage());
			throw e;
		}
	}

	public static void executeCleanShell(String type) throws Exception{
		String shellPath = PropertyUtil.getValue("file.path");
		shellPath = shellPath + "/script/";
		try {
			long start = System.currentTimeMillis();
			if(FileName.CUSTOMER_INFO.getValue().equals(type)){
				shellPath = shellPath + FileName.CLEAN_INFO_SHELL.getValue();
			}else if(FileName.ORDER.getValue().equals(type)){
				shellPath = shellPath + FileName.CLEAN_ORDER_SHELL.getValue();
			}else if(FileName.CUSTOMER_MOBILE.getValue().equals(type)){
				shellPath = shellPath + FileName.CLEAN_PHONE_SHELL.getValue();
			}
			Process process = Runtime.getRuntime().exec(shellPath);
		 	process.waitFor(90L,TimeUnit.MINUTES);
			//Thread.sleep(1000);
			 long end = System.currentTimeMillis();
			logger.info("执行清理脚本结束.type " + type + ", 耗时: " + (end-start) + "ms");
		} catch (Exception e) {
			logger.error("执行清理脚本失败, type: " + type + ",shellPath: " + shellPath +",error: " + e.getMessage());
			throw e;
		}
	}
}
